import Decimal from "decimal.js";
import { isType } from "@/utils/index";
/**
 * 列表或字段相加
 * @param value1 {Array<Sting>|Number|String} 数组对象或具体的值
 * @param value2 {String|Number} {
 *     1.如果value1为数组对象，则value2为value1中对应需要相加的key;
 *     2.如果value1为数组，内容为数字或字符串，则value2可不传
 *     3.如果value1为字符串或数字，则value2为两位值相加
 * }
 * @param decimals 保留位数，默认两位
 * @return {*}
 */
export function plusWith(
  value1: Array<string | number> | number | string,
  value2?: string | number,
  decimals: number = 2,
): number {
  let result: Decimal = new Decimal(0);
  if (Array.isArray(value1) && value1.length) {
    result = value1.reduce((a: any, b: any): Decimal => {
      if (b instanceof Object) {
        return Decimal.add(a ?? 0, b[value2 as string] ?? 0);
      }
      return Decimal.add(a ?? 0, b ?? 0);
    }, 0);
  }
  if (typeof value1 === "string" || (typeof value1 === "number" && (typeof value2 === "string" || typeof value2 === "number"))) {
    result = Decimal.add(value1, value2 as string | number);
  }
  return +result.toFixed(decimals);
}

/**
 * 两个值相减
 * @param value1 {String|Number} 字段1
 * @param value2 {String|Number} 字段2
 * @param decimals 需要保留的位数，默认两位
 * @return {number}
 */
export function subWith(value1: number | string, value2: number | string, decimals: number = 2) {
  let res = "0";
  if (typeof value1 === "string" || (typeof value1 === "number" && (typeof value2 === "string" || typeof value2 === "number"))) {
    res = Decimal.sub(value1, value2).toFixed(decimals);
  }
  return +res;
}

/**
 * 两个值相乘
 * @param value1 {String|Number} 字段1
 * @param value2 {String|Number} 字段2
 * @param decimals 需要保留的位数，默认两位
 * @return {number}
 */
export function mulWith(value1: number | string, value2: number | string, decimals: number = 2) {
  let res = "0";
  if (typeof value1 === "string" || (typeof value1 === "number" && (typeof value2 === "string" || typeof value2 === "number"))) {
    res = Decimal.mul(value1, value2).toFixed(decimals);
  }
  return +res;
}

/**
 * 两个值相除
 * @param value1 {String|Number} 字段1
 * @param value2 {String|Number} 字段2
 * @param decimals 需要保留的位数，默认两位
 * @return {number}
 */
export function divWith(value1: number | string, value2: number | string, decimals: number = 2) {
  let res: string = "0";
  if (typeof value1 === "string" || (typeof value1 === "number" && (typeof value2 === "string" || typeof value2 === "number"))) {
    res = Decimal.div(value1, value2).toFixed(decimals);
  }
  return +res;
}

/**
 * 金额转换，元=>万元、亿元
 * @param val
 * @param unit
 * @param digit
 * @param emptyTxt
 * @returns
 */
export function formatNum(val: number, unit?: number | "million" | "billion", digit = 2, emptyTxt = "0.00") {
  if (val && !isNaN(val)) {
    let num = 1;
    if (isType(unit) === "number") {
      num = unit as number;
    } else if (isType(unit) === "string") {
      if (unit === "billion") {
        num = 100000000;
      } else {
        num = 10000;
      }
    }
    return Decimal.div(val, num).toFixed(digit);
  }
  return emptyTxt;
}

/**
 * 金额自动转化 元转化万元或者亿元
 * @param val
 * @return {value unit}
 */
export function formatNumAuto(val: number, deci: number = 2) {
  let deal = {
    value: "0",
    unit: "元",
  };
  if (val && !isNaN(val)) {
    let money = Number(val);
    if (val < 10000) {
      deal.value = money.toFixed(deci);
      deal.unit = "元";
    } else if (val >= 10000 && val < 100000000) {
      deal.value = (money / 10000).toFixed(deci);
      deal.unit = "万元";
    } else if (val >= 100000000) {
      deal.value = (money / 100000000).toFixed(deci);
      deal.unit = "亿元";
    } else {
      deal.value = money.toFixed(deci);
      deal.unit = "元";
    }
  }
  return deal;
}
